package com.example.shortmessage.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.example.shortmessage.adapter.MyFragmentAdapter;
import com.example.shortmessage.base.BaseActivity;
import com.example.shortmessage.contract.MainContract;
import com.example.shortmessage.data.constant.MainConst;
import com.example.shortmessage.databinding.ActivityMainBinding;
import com.example.shortmessage.presenter.MainPresenter;
import com.example.shortmessage.tips.dialog.WaitingDialog;
import com.example.shortmessage.ui.fragment.MessageFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;


/**
 * @author 27638
 * 主界面
 */
public class MainActivity extends BaseActivity<ActivityMainBinding, MainContract.View<MainContract.Presenter>,MainContract.Presenter>
        implements MainContract.View<MainContract.Presenter>, View.OnClickListener {

    public static void MainActivityInstance(Context context){
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected MainContract.Presenter initPresenter() {
        return new MainPresenter(this);
    }

    @NonNull
    @Override
    protected ActivityMainBinding getLayoutView() {
        return ActivityMainBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView(){
        mPresenter.upData();
        String[] titles = {"未读", "已读",};
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new MessageFragment(mPresenter.loadData(MainConst.UNREAD)));
        fragmentList.add(new MessageFragment(mPresenter.loadData(MainConst.READ)));
        MyFragmentAdapter myFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(), getLifecycle(), fragmentList);
        mBinding.vpMessage.setAdapter(myFragmentAdapter);
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(mBinding.title.tabTitle, mBinding.vpMessage, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(titles[position]);
            }
        });
        tabLayoutMediator.attach();
        mBinding.fabMessage.setOnClickListener(this);
    }

    @Override
    protected void setStyle() {
        mBinding.title.tabTitle.setVisibility(View.VISIBLE);
    }


    @Override
    public void onClick(View v) {
        Uri smsToUri = Uri.parse("smsto:");
        Intent intent = new Intent(Intent.ACTION_SENDTO,smsToUri);
        startActivity(intent);
    }


    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /*************************************************************/
    @Override
    public void setPresenter(@NonNull MainContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showLoading() {
        WaitingDialog.getInstance().show(this);
    }

    @Override
    public void hideLoading() {
        WaitingDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }
}
